/**
 * Created by xiebang
 * Description
 * User:小谢
 * Date:2021-09-02
 * Time:19:53
 */

class ListNodeD {
    public int data;
    public ListNodeD prev;
    public ListNodeD next;

    public ListNodeD(int data) {
        this.data = data;
    }
}

public class MyRealLinkedList {
    public ListNodeD head;//头
    public ListNodeD last;//尾

    //头插法
    public void addFirst(int data) {
        ListNodeD node = new ListNodeD(data);
        if(head == null){
            head = node;
            last = node;
        }else {
            node.next = head;
            head.prev = node;
            head =node;
        }
    }
    //尾插法
    public void addLast(int data) {
        ListNodeD node = new ListNodeD(data);
        if(head == null){
            head = node;
            last = node;
        }else {
            last.next = node;
            node.prev = last;
            last = node;
        }
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data) {
        ListNodeD cur = this.head;

        if (index < 0 || index > size()){
            System.out.println("坐标非法");
            return;
        }
        if (index == 0){
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        ListNodeD node = new ListNodeD(data);
        while (index != 0) {
            cur = cur.next;
            index--;
        }
        node.next = cur;
        node.prev = cur.prev;
        cur.prev.next = node;
        cur.prev = node;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        if (this.head == null)
            return false;

        ListNodeD ret = this.head;
        while(ret!= null) {
            if(ret.data == key) {
                return true;
            }
            ret = ret.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
//    public void remove(int key) {
//        ListNodeD cur = this.head;
//        while (cur!= null){
//            if(cur.data == key){
//                if(cur == this.head){
//                    this.head = this.head.next;
//                    if (this.head == null) {
//                        this.last = null;
//                    }
//                } else {
//                    cur.prev.next = cur.next;
//                    if (cur.next == null){
//                        this.last == cur.prev;
//                    }
//                }
//            }
//        }
//    }
    //删除所有值为key的节点
    public void removeAllKey(int key) {

    }
    //得到单链表的长度
    public int size() {
        ListNodeD cur = this.head;
        if(cur == null)
        {
            return 0;
        }
        int size = 0;
        while(cur!= null)
        {
            size++;
            cur = cur.next;
        }
        return size;
    }

    public void display() {
        if(this.head == null) return;
        ListNodeD cur = this.head;
        while(cur!= null) {
            System.out.print(cur.data + " ");
            cur = cur.next;
        }
    }

    public void clear() {
        ListNodeD cur = this.head;

        while (cur!= null){
            ListNodeD nextd = cur.next;
            cur.next = null;
            cur.prev = null;
            cur = nextd;
        }
        this.head = null;
        this.last = null;
    }


}